﻿using System;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;
using System.Data;
using System.Web.UI.WebControls;

namespace Besture.UI
{
   
    public partial class RoomSet : BasePage
    {
        public string HtmlRoomFacilities = string.Empty;

        #region 页面加载事件
        
        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            //页面加载
            InitPage();
            //编辑编号
            InitData(ID);

            
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {
            //绑定酒店
            var dt = new HotelsDac().GetDataTable();
            ControlHelper.BindDropDownList(ddlHotelsID,dt,"HotelsName","ID");

            //绑定床型
            dt = new RoomCategoryDac().GetDataTableByCategoryType(Constants.ROOM_CATEGORY_BEDTYPE);
            ControlHelper.BindDropDownList(ddlBedType, dt, "CategoryName", "ID");

            //绑定宽带
            dt = new RoomCategoryDac().GetDataTableByCategoryType(Constants.ROOM_CATEGORY_BROADBAND);
            ControlHelper.BindDropDownList(ddlBroadband, dt, "CategoryName", "ID");

            //绑定无烟
            dt = new RoomCategoryDac().GetDataTableByCategoryType(Constants.ROOM_CATEGORY_SMOKE);
            ControlHelper.BindDropDownList(ddlSmoke, dt, "CategoryName", "ID");

            //绑定房型设施
            if (ID == 0)
                NewMethod(null);
            
            //早餐情况 绑定
            DataTable vBreakfast = QueryDac.GetTable("SELECT * FROM TBDictionaryType WHERE Type = 5");
            BindDropDownList(ddlBreakfast, vBreakfast, "name", "code");

            //顺序
            txtSortID.Text = SortService.GetSortID("tbroom", true);
        }

        private void NewMethod(string sRoomFacilities)
        {
           
            //绑定房型设施
           var  dt = new DictionaryTypeDac().GetDataTableByType(8);
            if (dt != null)
            {
                HtmlRoomFacilities += "<img src=\"/Admin/Themes/Images/indispensable.gif\" align=\"absMiddle\">";
                foreach (DataRow dr in dt.Rows)
                {
                    string checkedBool = string.Empty;
                    if (!string.IsNullOrWhiteSpace(sRoomFacilities))
                    {
                        if (sRoomFacilities.Contains(dr["Code"].ToString()))
                        {
                            checkedBool = "checked='checked'";
                        }   
                    }   
                    HtmlRoomFacilities += string.Format("<input name='cbRoom' type='checkbox' {2} value='{0}' />{1}&nbsp;&nbsp;", dr["Code"].ToString(), dr["Name"].ToString(),checkedBool);
               
                }
                cbRoomFacilities.InnerHtml = HtmlRoomFacilities;
            }     
        }

        private static void BindDropDownList(DropDownList dropDownList, DataTable sourceTable, string dataTextField,
         string dataValueField)
        {
            if (sourceTable != null)
            {
                dropDownList.DataSource = sourceTable;
                dropDownList.DataTextField = dataTextField;
                dropDownList.DataValueField = dataValueField;
                dropDownList.DataBind();
            }
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            if (id == 0) return;

            var dac = new RoomDac();
            var model = dac.GetModel(id);
			if(model==null) return;

            ddlHotelsID.SelectedValue = model.HotelsID.ToString() == "" ? "" : model.HotelsID.ToString();
			txtRoomName.Text = model.RoomName.ToString();
			imgRoomImage.ImageUrl = model.RoomImage.ToString();
			ddlBedType.SelectedValue = model.BedType.ToString() == "" ? "" : model.BedType.ToString();
			txtBedTypeRemark.Text = model.BedTypeRemark.ToString();
			ddlBreakfast.SelectedValue = model.Breakfast.ToString() == "" ? "" : model.Breakfast.ToString();
			ddlBroadband.SelectedValue = model.Broadband.ToString() == "" ? "" : model.Broadband.ToString();
			txtRoomArea.Text = model.RoomArea.ToString();
			txtFloor.Text = model.Floor.ToString();
			//txtLowPrice.Text = model.LowPrice.ToString();
			txtPeopleNumber.Text = model.PeopleNumber.ToString();
			txtRoomRemark.Text = model.RoomRemark.ToString();
			ddlSmoke.SelectedValue = model.Smoke.ToString() == "" ? "" : model.Smoke.ToString();
			txtSortID.Text =Convert.ToString(model.SortID != 0 ? model.SortID : 0);
            //绑定多图
            UploadMultiPhoto.InitPhotoTable = new ImageDac().GetDataTableByImageType(model.ID, Constants.IMAGE_TYPE_ROOM);
            hidRoomFacilities.Value = model.RoomFacilities;
            //绑定房型设施
            NewMethod(model.RoomFacilities);
        }

        #endregion

        #region 保存

        protected void BtnSubmitClick(object sender, EventArgs eventArgs)
        {
            //多图
            UploadMultiPhotoModel uploadMultiPhotoModel = new UploadMultiPhotoModel();
            //删除图片集合
            uploadMultiPhotoModel.DelIdArgs = StringHelper.SplitString(UploadMultiPhoto.DelIdArgs.TrimEnd(','));
            //添加图片集合
            uploadMultiPhotoModel.AddImgUrlArgs = StringHelper.SplitString(UploadMultiPhoto.AddImgUrlArgs);
            uploadMultiPhotoModel.AddImgRemarParamIDkArgs = StringHelper.SplitString(UploadMultiPhoto.AddImgRemarParamIDkArgs);
            //更新图片集合
            uploadMultiPhotoModel.ImgIdArgs = StringHelper.SplitString(UploadMultiPhoto.ImgIdArgs.TrimEnd(','));
            uploadMultiPhotoModel.ImgUrlArgs = StringHelper.SplitString(UploadMultiPhoto.ImgUrlArgs.TrimEnd(','));
            uploadMultiPhotoModel.ImgRemarParamIDkArgs = StringHelper.SplitString(UploadMultiPhoto.ImgRemarkArgs);
            uploadMultiPhotoModel.ImgId = StringHelper.SplitString(UploadMultiPhoto.ImgId);

            var dac = new RoomDac();
            RoomModel model=null;
            var baseDac = new BaseDac<RoomModel>();
            //添加
            if (!EditPage)
            {
                model = new RoomModel();
                model.HotelsID = ddlHotelsID.SelectedValue == "" ? 0 : Convert.ToInt32(ddlHotelsID.SelectedValue);
				model.RoomName = txtRoomName.Text.Trim();
				model.RoomImage = imgRoomImage.ImageUrl.ToString();
				model.BedType = ddlBedType.SelectedValue == "" ? 0 : Convert.ToInt32(ddlBedType.SelectedValue);
				model.BedTypeRemark = txtBedTypeRemark.Text.Trim();
                //早餐情况
                model.Breakfast = ddlBreakfast.SelectedValue == "" ? 0 : Convert.ToInt32(ddlBreakfast.SelectedValue);

				model.Broadband = ddlBroadband.SelectedValue == "" ? 0 : Convert.ToInt32(ddlBroadband.SelectedValue);
				model.RoomArea = Convert.ToInt32(txtRoomArea.Text.Trim());
				model.Floor =  txtFloor.Text.Trim();
			//	model.LowPrice =  Convert.ToInt32(txtLowPrice.Text.Trim());
                model.PeopleNumber = Convert.ToInt32(txtPeopleNumber.Text.Trim());
				model.RoomRemark = txtRoomRemark.Text.Trim();
				model.Smoke = ddlSmoke.SelectedValue == "" ? 0 : Convert.ToInt32(ddlSmoke.SelectedValue);
                model.SortID = Convert.ToInt32(txtSortID.Text.Length > 0 ? txtSortID.Text : "0");
                model.RoomFacilities = string.IsNullOrEmpty(hidRoomFacilities.Value) ? "":hidRoomFacilities.Value.TrimEnd(',');
                //加载添加基本数据
                baseDac.LoadAddBaseModel(model);
                dac.Add(model, uploadMultiPhotoModel);
            }
            //编辑
            else
            {
                model = dac.GetModel(ID);
                model.HotelsID = ddlHotelsID.SelectedValue == "" ? 0 : Convert.ToInt32(ddlHotelsID.SelectedValue);
                model.RoomName = txtRoomName.Text.Trim();
                model.RoomImage = imgRoomImage.ImageUrl.ToString();
                model.BedType = ddlBedType.SelectedValue == "" ? 0 : Convert.ToInt32(ddlBedType.SelectedValue);
                model.BedTypeRemark = txtBedTypeRemark.Text.Trim();
                //早餐情况
                model.Breakfast = ddlBreakfast.SelectedValue == "" ? 0 : Convert.ToInt32(ddlBreakfast.SelectedValue);

                model.Broadband = ddlBroadband.SelectedValue == "" ? 0 : Convert.ToInt32(ddlBroadband.SelectedValue);
                model.RoomArea = Convert.ToInt32(txtRoomArea.Text.Trim());
                model.Floor = txtFloor.Text.Trim();
             //   model.LowPrice = Convert.ToInt32(txtLowPrice.Text.Trim());
                model.PeopleNumber = Convert.ToInt32(txtPeopleNumber.Text.Trim());
                model.RoomRemark = txtRoomRemark.Text.Trim();
                model.Smoke = ddlSmoke.SelectedValue == "" ? 0 : Convert.ToInt32(ddlSmoke.SelectedValue);
                model.SortID = Convert.ToInt32(txtSortID.Text.Length > 0 ? txtSortID.Text : "0");
                model.RoomFacilities = string.IsNullOrEmpty(hidRoomFacilities.Value) ? "":hidRoomFacilities.Value.TrimEnd(',');
                //加载修改基本数据
                baseDac.LoadUpdateBaseModel(model);
                dac.Update(model, uploadMultiPhotoModel);
            }
            FunHelper.Alert("保存成功", ListUrl());
        }

        #endregion
    }
}